# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
    "//pw_build:pigweed.bzl",
    "pw_cc_binary",
    "pw_cc_library",
    "pw_cc_test",
)
load("//pw_protobuf_compiler:proto.bzl", "pw_proto_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

pw_cc_library(
    name = "headers",
    hdrs = [
        "public/pw_trace_tokenized/config.h",
        "public/pw_trace_tokenized/internal/trace_tokenized_internal.h",
        "public/pw_trace_tokenized/trace_callback.h",
        "public/pw_trace_tokenized/trace_tokenized.h",
        "public_overrides/pw_trace_backend/trace_backend.h",
    ],
    includes = [
        "public",
        "public_overrides",
    ],
    deps = [
        "//pw_preprocessor",
        "//pw_tokenizer",
    ],
)

pw_cc_library(
    name = "linux_config_overrides",
    hdrs = ["linux_config_overrides.h"],
    tags = ["manual"],
)

pw_cc_library(
    name = "pw_trace_tokenized",
    srcs = [
        "trace.cc",
    ],
    deps = [
        ":headers",
        "//pw_assert",
        "//pw_log",
        "//pw_status",
        "//pw_tokenizer",
        "//pw_trace:facade",
        "//pw_varint",
    ],
)

pw_cc_library(
    name = "trace_rpc_service",
    srcs = [
        "trace_rpc_service_nanopb.cc",
    ],
    hdrs = [
        "public/pw_trace_tokenized/trace_rpc_service_nanopb.h",
    ],
    includes = [
        "public",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":buffer",
        ":protos_cc.nanopb_rpc",
        "//pw_log",
        "//pw_trace",
    ],
)

pw_cc_library(
    name = "trace_buffer_headers",
    hdrs = [
        "public/pw_trace_tokenized/trace_buffer.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":pw_trace_tokenized",
        "//pw_ring_buffer",
        "//pw_status",
    ],
)

pw_cc_library(
    name = "buffer",
    srcs = [
        "trace_buffer.cc",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":trace_buffer_headers",
        "//pw_ring_buffer",
        "//pw_status",
    ],
)

pw_cc_library(
    name = "buffer_log",
    srcs = [
        "trace_buffer_log.cc",
    ],
    hdrs = [
        "public/pw_trace_tokenized/trace_buffer_log.h",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":trace_buffer_headers",
        "//pw_base64",
        "//pw_log",
        "//pw_string",
    ],
)

proto_library(
    name = "protos",
    srcs = [
        "pw_trace_protos/trace.proto",
        "pw_trace_protos/trace_rpc.proto",
    ],
    # TODO(tpudlik): We should provide trace_rpc.options to nanopb here, but the
    # current proto codegen implementation provides no mechanism for doing so.
    # inputs = [ "pw_trace_protos/trace_rpc.options" ]
)

pw_proto_library(
    name = "protos_cc",
    deps = [":protos"],
)

pw_cc_library(
    name = "pw_trace_tokenized_fake_time",
    srcs = [
        "fake_trace_time.cc",
    ],
    deps = [
        ":pw_trace_tokenized",
    ],
)

pw_cc_test(
    name = "trace_tokenized_test",
    srcs = [
        "trace_test.cc",
    ],
    includes = [
        "pw_trace_test",
        "pw_trace_test/public_overrides",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":pw_trace_tokenized",
        "//pw_preprocessor",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "buffer_test",
    srcs = [
        "trace_buffer_test.cc",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":buffer",
        ":pw_trace_tokenized",
        "//pw_preprocessor",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "buffer_log_test",
    srcs = [
        "trace_buffer_log_test.cc",
    ],
    # TODO(b/260641850): Get pw_trace_tokenized building in Bazel.
    tags = ["manual"],
    deps = [
        ":buffer_log",
        ":pw_trace_tokenized",
        "//pw_preprocessor",
        "//pw_unit_test",
    ],
)

pw_cc_library(
    name = "pw_trace_host_trace_time",
    srcs = ["host_trace_time.cc"],
    includes = ["example/public"],
    deps = [":pw_trace_tokenized"],
)

pw_cc_library(
    name = "pw_trace_example_to_file",
    hdrs = ["example/public/pw_trace_tokenized/example/trace_to_file.h"],
    includes = ["example/public"],
    deps = ["//pw_trace"],
)

pw_cc_binary(
    name = "trace_tokenized_example_basic",
    srcs = ["example/basic.cc"],
    # TODO(b/258071921): Fix puzzling compiler errors
    tags = ["manual"],
    deps = [
        ":pw_trace_example_to_file",
        "//pw_log",
        "//pw_trace",
        "//pw_trace:pw_trace_sample_app",
    ],
)

pw_cc_binary(
    name = "trace_tokenized_example_trigger",
    srcs = ["example/trigger.cc"],
    # TODO(b/258071921): Fix puzzling compiler errors
    tags = ["manual"],
    deps = [
        ":pw_trace_example_to_file",
        "//pw_log",
        "//pw_trace",
        "//pw_trace:pw_trace_sample_app",
    ],
)

pw_cc_binary(
    name = "trace_tokenized_example_filter",
    srcs = ["example/filter.cc"],
    # TODO(b/258071921): Fix puzzling compiler errors
    tags = ["manual"],
    deps = [
        ":pw_trace_example_to_file",
        "//pw_log",
        "//pw_trace",
        "//pw_trace:pw_trace_sample_app",
    ],
)

pw_cc_library(
    name = "trace_tokenized_example_rpc",
    srcs = ["example/rpc.cc"],
    # TODO(b/258071921): Fix puzzling compiler errors
    tags = ["manual"],
    deps = [
        "//pw_hdlc",
        "//pw_log",
        "//pw_rpc",
        "//pw_rpc/system_server",
        "//pw_trace",
        "//pw_trace:pw_trace_sample_app",
    ],
)

pw_cc_library(
    name = "trace_tokenized_example_linux_group_by_tid",
    srcs = ["example/linux_group_by_tid.cc"],
    tags = ["manual"],
    deps = [
        ":pw_trace_example_to_file",
        "//pw_log",
        "//pw_trace",
    ],
)
